package until
import (
		// "net/http"
		"encoding/json"
		// "strconv"
		"time"
		// "github.com/dgrijalva/jwt-go"
		// "github.com/labstack/echo"
		"gopkg.in/mgo.v2/bson"
		// "github.com/jinzhu/gorm"
		
		"oxy_server/module/log"
		// "oxy_server/model"
)

func JsonToBson(jsonstr string)(interface{},error)  {
	// var q interface{}
	q := make(map[string]interface{})
	M := bson.M{}
	if jsonstr != ""{
		err :=json.Unmarshal([]byte(jsonstr),&q)
		if err != nil {
			log.Info("json 字符串转interface",err)
			return nil,err
		}
		if q["newday"] != nil{
			a:=q["newday"].(map[string]interface{})
			if a["$gte"] != nil{
				a["$gte"] = FormatTime(a["$gte"].(string))
			}
			if a["$lte"] != nil{
				a["$lte"] = FormatTime(a["$lte"].(string))
			} 
			q["newday"] = a
		}
		data, err1 := bson.Marshal(q)
		if err1 != nil {
			log.Info("interface ->字符流",err)
			return nil,err
		}
		err = bson.Unmarshal(data,&M)
		if err!= nil {
			log.Info("字符流->bson.M",err)
			return nil,err		
		}
		
	}else{
		M = nil
	}
	return M,nil
}

func FormatTime( value string) time.Time {
	theTime, _:= time.Parse(time.RFC3339, value)
	return theTime
}
// 通过map主键唯一的特性过滤重复元素
func RemoveRepByMap(slc []string) []string {
    result := []string{}
    tempMap := map[string]byte{}  // 存放不重复主键
    for _, e := range slc{
        l := len(tempMap)
        tempMap[e] = 0
        if len(tempMap) != l{  // 加入map后，map长度变化，则元素不重复
            result = append(result, e)
        }
    }
    return result
}

func ParseTime(toBeCharge string) int64{
	timeLayout := "2006-01-02 15:04:05"                             //转化所需模板
	loc, _ := time.LoadLocation("Local")                            //重要：获取时区
	theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型
	return theTime.Unix() 
}